package hospital.order.api;

import hospital.order.service.PaymentService;
import hospital.order.service.WeiXinService;
import hospital.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author
 * @description: 微信支付接口
 */
@RestController
@RequestMapping("/api/order/weixin")
public class WeiXinApiController {
    @Autowired
    private WeiXinService weiXinService;
    @Autowired
    private PaymentService paymentService;
    
    /**
     * @Author yang
     * @Description //TODO 生成二维码接口
     * @Param
     * @return 
     **/
    @GetMapping("createNative/{orderId}")
    public Result createNative(@PathVariable String orderId) {
        Map map = weiXinService.createNative(orderId);
        return Result.ok(map);
    }
    
    /**
     * @Author yang
     * @Description //TODO 查询支付状态
     * @Param
     * @return 
     **/
    @ApiOperation("查询支付状态")
    @GetMapping("queryPayStatus/{orderId}")
    public Result queryPayStatus(@PathVariable String orderId){
        //1.调用微信接口，查询订单的支付状态
        Map<String,String> resultMap = weiXinService.queryPayStatus(orderId);
        //2.如果resultMap为空，则支付失败
        if(null==resultMap){
            return Result.fail().message("支付出错");
        }
        if("SUCCESS".equals(resultMap.get("trade_state"))) { //支付成功
            //更新订单状态
            String out_trade_no = resultMap.get("out_trade_no");//订单编码
            //更新订单表，支付表，医院的状态
            paymentService.paySuccess(out_trade_no,resultMap);
            return Result.ok().message("支付成功");
        }
        return Result.ok().message("支付中");
    }
}
